package com.dcits.account.controller;

import com.dcits.account.common.Msg;
import com.dcits.account.entity.cto.InputObject;
import com.dcits.account.entity.cto.OutputObject;
import com.dcits.account.service.interfaces.IUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @Auther: 2843896844@qq.com
 * @Date: 2021/06/11/19:52
 * @Description: 登录注册相关接口
 */
@Controller
public class LoginController {

    private Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private IUserService iUserService;

    @ResponseBody
    @GetMapping(value = "/login/loginVerifyUserPhone/{userphone}")
    public OutputObject loginVerifyUserPhone(
            @PathVariable String userphone
    ) {
        InputObject inputObject = new InputObject();
        OutputObject outputObject = new OutputObject();
        inputObject.put("userPhone", userphone);
        iUserService.loginVerifyUserphone(inputObject, outputObject);
        return outputObject;
    }

    @ResponseBody
    @PostMapping(value = "/login/verifyLogin")
    public OutputObject verifyLogin(
            String userPhone,
            String password,
            HttpServletRequest request
    ) {
        InputObject inputObject = new InputObject();
        OutputObject outputObject = new OutputObject();
        inputObject.put("userPhone", userPhone);
        inputObject.put("password", password);
        iUserService.verifyLogin(inputObject, outputObject);
        if (!outputObject.isFailed()) {
            HttpSession session = request.getSession();
            session.setAttribute("user", outputObject.getBean().get("user"));
            outputObject.getBean().put("url", "/dcits/index");
        }
        return outputObject;
    }

    @ResponseBody
    @PostMapping(value = "/doRegister")
    public OutputObject doRegister(
            String username,
            String realName,
            String userPhone,
            String email,
            String password
    ) {
        InputObject inputObject = new InputObject();
        OutputObject outputObject = new OutputObject();
        inputObject.put("username", username);
        inputObject.put("realName", realName);
        inputObject.put("userPhone", userPhone);
        inputObject.put("email", email);
        inputObject.put("password", password);
        iUserService.register(inputObject, outputObject);
        if (!outputObject.isFailed()) {
            outputObject.getBean().put("url", "/dcits/login");
        }
        return outputObject;
    }

}
